//
//  LWWelcomViewController.swift
//  Microblog
//
//  Created by Roman on 17/1/8.
//  Copyright © 2017年 Roman. All rights reserved.
//  用户欢迎界面

import UIKit
import SDWebImage


class LWWelcomViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white

        // Do any additional setup after loading the view.
        setupUI()
    }
    
    //在视图已经显示里面进行跟新
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }

    func setupUI()  {
        view.addSubview(iconView)
        view.addSubview(label)
        //为了显示布局,直接在rootVC修改下
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        label.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(iconView.snp.bottom).offset(20)
        }
        //切圆角
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        
        //设置网络图片
       // let url = URL(string: LWUserAccountViewModel.shard.account?.avatar_large ?? "")
        
        iconView.sd_setImage(with: LWUserAccountViewModel.shard.headURL)
        
    }
    //动画效果
    func startAnimation(){
        
        self.iconView.snp.updateConstraints({ (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-(ScreenHeight/2 + 160))
        })
        
        UIView.animate(withDuration: 1.2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 7, options: [], animations: {
            //修改iconView 的位置 ,修改约束
            
            
            //强制跟新约束
            self.view.layoutIfNeeded()
            
        }) { (_) in
            
            UIView.animate(withDuration: 0.5, animations: { 
                self.label.alpha = 1
            }, completion: { (_) in
                print("-----ok")
                //执行回调.
               // UIApplication.shared.keyWindow?.rootViewController = LWTabBarViewController()
                NotificationCenter.default.post(name: NSNotification.Name.AppChangRootVC, object: "LWTabBarViewController")
            })
            
        }
    }
    
    
    
    //头像
    lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default"))
    
    
    //文字
    lazy var label:UILabel = {
        let label = UILabel(text: "\(LWUserAccountViewModel.shard.account?.screen_name ?? "")欢迎回来", fontSize: 15, textColor: UIColor.darkGray)
        label.alpha = 0
        return label
    }()
    

   

}
